//校验身份证号码
function sfz15to18(com,csrq,xb)
{
   var sfz15 = com.value;
   var sfzcode="";
   var tempStr = "";
   var wh = "";
   if(sfz15.length == 0) return "";
   if (sfz15.length == 15)
   {
      tempStr = sfz15.substring(0, 6) + "19" + sfz15.substring(6);
      wh = sfzwh(tempStr);
      sfzcode = tempStr + wh;
   }
   else if (sfz15.length == 17)
   {
      wh = sfzwh(sfz15);
      sfzcode = sfz15 + wh;
   }
   else if (sfz15.length  == 18)
   {
      tempStr = sfz15.substring(0,17);
      wh = sfzwh(tempStr);
      sfzcode = tempStr + wh;

       if (sfzcode!=sfz15)
       {
          alert("身份证号校验位错误:"+sfz15+",正确的号码是："+sfzcode);
          com.value=sfzcode;
          com.focus();
          return;
       }
   }
   else
   {
      alert("身份证号长度不正确");
      com.value=sfzcode;
      com.focus();
      return;
   }
   
   if(csrq!=null&&csrq!=""){
   		var csrqStr = sfzcode.substring(6,14);
   		if(csrqStr!=csrq){
   			alert("身份证号与出生日期不符合");
   			com.value=sfzcode;
   			com.focus();
   			return;
   		}
   		
   }
   
   if(xb!=null&&xb!=""){
   		var xbStr = sfzcode.substring(16,17);
   		
   		if(xb=='1'){
   			if(xbStr%2==0){
   				alert("身份证号与性别不符合");
   				com.value=sfzcode;
   				com.focus();
   				return;
   			}
   		}
   		if(xb=='2'){
   			if(xbStr%2!=0){
   				alert("身份证号与性别不符合");
   				com.value=sfzcode;
   				com.focus();
   				return;
   			}
   		}	
   }
   
   com.value=sfzcode;
}

//获得尾号
function sfzwh(code)
{
 var retStr = '0';
 var bodycode = new Array(19);
 var codeev = new Array(19);
 var i, sum=0;
 var keycode, keycode1;
 var codevalue = [
    '1', '0', 'X', '9', '8', '7', '6', '5', '4', '3', '2'];
 var  weight =
    [
    0, 1, 2, 4, 8, 5, 10, 9, 7, 3, 6, 1, 2, 4, 8, 5, 10, 9, 7];

 try
 {
    for (var i = 0; i < 19; i++)
    {
       bodycode[i] = 0;
       codeev[i] = 0;
    }

    for (var i = 2; i <= 18; i++)
    {
       codeev[i] = code.charCodeAt(18 - i);
       bodycode[i] = (codeev[i] - 0x30);
    }

    for (sum = 0, i = 2; i <= 18; i++)
    {
       sum += bodycode[i] * weight[i];
    }

    keycode = (sum % 11);

    if (keycode < 0 || keycode > 11) return  - 1;
    keycode1 = codevalue[keycode];
    return keycode1;

 }
 catch (e)
 {
    alert("error"+e);
 }

 return retStr;
}

//检查日期
function checkdate(com,xtrq){
    if (formatDate(com)){
        if(xtrq){
    		if(com.value>xtrq){
    			alert("输入时期大于当前日期");
    			com.value="";
    			com.focus();
    		}else{
        		com.value=com.value;
        	}
        }else{
        	com.value=com.value;
        }
    }else{
        com.value="";
    }
}
function formatDate(obj, obj2){
   if(obj.value.length!=0)
     if((obj.value.length!=8)&(obj.value.length!=10)){
            alert("'填入的日期'不符合标准,或者无效,具体格式如下:"+"\n"+"四位年+两位月份+两位日期"+"\n"+"例:20080101");
            obj.focus();
            return false;
     }
     else{
        var c;
        var d;
        if(obj.value.length==8)
         c = obj.value;
        else{
          d = obj.value;

          var x = d.charAt(4);

          var y = d.charAt(7);

           var pa=new Object();
           pa.a=/^[0-9]$/;
          var e=pa["a"];
          var g=e.test(x);
          var h=e.test(y);
          if(g|h){
                alert("'填入的日期'不符合标准,或者无效,具体格式如下:"+"\n"+"四位年+两位月份+两位日期"+"\n"+"例:20080101");
                obj.focus();
                return false;

          }
          else

          c = d.charAt(0)+d.charAt(1)+d.charAt(2)+d.charAt(3)+d.charAt(5)+d.charAt(6)+d.charAt(8)+d.charAt(9);

         }
                    var pa=new Object();
            pa.a=/^[1-2][0-9]{3}(([0][1-9])|([1][0-2]))(([0][1-9])|([1-2][0-9])|([3][0-1]))$/;
            //c=obj.value;
            var d=pa["a"];
            var f=d.test(c);
            if(!f)
            {
                    alert("'填入的日期'不符合标准,或者无效,具体格式如下:"+"\n"+"四位年+两位月份+两位日期"+"\n"+"例:20080101");
                    obj.focus();
                    return false;
            }
            else
               {
                    var year = c.charAt(0)+c.charAt(1)+c.charAt(2)+c.charAt(3);
                    var month = c.charAt(4)+c.charAt(5);
                    var day = c.charAt(6)+c.charAt(7);



                    if(((month=='04')|(month=='06')|(month=='09')|(month=='11'))&(day=='31'))
                    {
                            alert(month+"月没有31号");
                            obj.focus();
                            return false;
                    }

                    if(month=='02')
                    {

                            if((day=='30')|(day=='31'))
                            {
                                    alert("2月没有这个日期");
                                    obj.focus();
                                    return false;
                            }
                            else
                            {

                                    if((!(((parseInt(year)%4=='0')&(parseInt(year)%100!='0'))|(parseInt(year)%400=='0')))&(day=='29'))
                                    {
                                            alert(year+"年不是闰年，2月没有这个日期");
                                            obj.focus();
                                            return false;
                                    }
                            }
                    }
                    var t = c.charAt(0)+c.charAt(1)+c.charAt(2)+c.charAt(3)+c.charAt(4)+c.charAt(5)+c.charAt(6)+c.charAt(7);
                    obj.value=t;
                    if (obj2 != null) obj2.value = t;

               }
    }
    return true;
}


function checkTime(com){
	var tempTime = com.value;
	if(tempTime.length!=0){
		if(tempTime.length!=6){
			alert("填入的时间不符合标准,或者无效,具体格式如下:"+"\n"+"两位时+两位分+两位秒"+"\n"+"例:080159");
			com.focus();
		}else{
			var hStr = tempTime.substring(0,2);
			if(hStr>23||hStr<0){
				alert("填入的小时不合法");
				com.focus();
			}
			var mStr = tempTime.substring(2,4);
			if(mStr>59||mStr<0){
				alert("填入的分钟不合法");
				com.focus();
			}
			var sStr = tempTime.substring(4,6);
			if(sStr>59||sStr<0){
				alert("填入的秒不合法");
				com.focus();
			}
		}
	}
}
